package com.undsf.compass;

import com.undsf.graphics.ExtBufferedImage;
import com.undsf.graphics.GraphicsExt;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;

/**
 * Created by Arathi on 2015/05/06.
 */
public class CompassDrawer {
    public static final int IMAGE_DEFAULT_WIDTH = 800;
    public static final int IMAGE_DEFAULT_HEIGHT = 800;
    public static final String OUTPUT_PATH = "C:\\temp\\compass\\";

    public static final String[] Trigrams = {"离","坤","兑","乾","坎","艮","震","巽"};
    public static final String[] CSBs = {"壬","子","癸","丑","艮","寅","甲","卯","乙","辰","巽","巳","丙","午","丁","未","坤","申","庚","酉","辛","戌","乾","亥"};

    private int imageWidth;
    private int imageHeight;

    private Color colorMain;
    private Font trigramFont;
    private Font csbFont;
    private Font degreeFont;
    private Font aspectFont;

    private int radius0;
    private int radius1;
    private int radius2;

    private int longScaleR;
    private int longScaleLength;
    private int shortScaleR;
    private int shortScaleLength;

    public CompassDrawer(){
        imageWidth = imageHeight = IMAGE_DEFAULT_HEIGHT;

        trigramFont = new Font("楷体", 0, 36);
        csbFont = new Font("楷体", 0, 36);
        aspectFont = new Font("楷体", 0, 30);
        degreeFont = new Font("Consolas", 0, 16);
        colorMain = Color.GREEN;

        radius0 = 200;
        radius1 = 265;
        radius2 = 310;

        longScaleR = 360;
        shortScaleR = 350;
        longScaleLength = 40;
        shortScaleLength = 20;
    }

    public RenderedImage draw(){
        ExtBufferedImage image = new ExtBufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = image.createGraphics();
        //消锯齿
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setColor(Color.GREEN);
        //修改一下原点
        g.translate(image.getWidth()/2, image.getHeight()/2);

        GraphicsExt gext = new GraphicsExt(g);

        Point o = new Point(0, 0);
        gext.drawCircle(o, radius2);
        gext.drawCross(o, 16);

        //先绘制内圈的八个汉字
        int fontSize = trigramFont.getSize();
        int offsetX=-fontSize/2, offsetY=-radius0+fontSize, offset=4;
        int size = Trigrams.length;
        g.setFont(trigramFont);
        for (int i=0; i<size; ++i){
            int index = (i + offset) % size;
            g.drawString(Trigrams[index], offsetX, offsetY);
            g.rotate(2.0 * Math.PI / size);
        }
        gext.drawCircle(o, radius0);

        //绘制外圈的24个汉字
        fontSize = csbFont.getSize();
        offsetX=-fontSize/2;
        offsetY=-radius0-10;
        offset = 4*3;
        size = CSBs.length;
        g.setFont(csbFont);
        fontSize = csbFont.getSize();
        g.rotate((size / 2 - 1) * 2.0 * Math.PI / size);
        for (int i=0; i<size; ++i){
            int index = (i + offset) % size;
            g.drawString(CSBs[index], offsetX, offsetY);
            g.rotate(2.0 * Math.PI / size);
        }
        g.rotate((size / 2 + 1) * 2.0 * Math.PI / size);
        gext.drawCircle(o, radius1);

        //东西南北四个方位
        g.setFont(aspectFont);
        fontSize = aspectFont.getSize();
        offsetX=-fontSize/2;
        offsetY=-radius1-6;
        g.drawString("北", offsetX, offsetY);
        g.rotate(2.0 * Math.PI / 8);
        g.drawString("東北", offsetX * 2, offsetY);
        g.rotate(2.0 * Math.PI / 8);
        g.drawString("東", offsetX, offsetY);
        g.rotate(2.0 * Math.PI / 8);
        g.drawString("東南", offsetX * 2, offsetY);
        g.rotate(2.0 * Math.PI / 8);
        g.drawString("南", offsetX, offsetY);
        g.rotate(2.0 * Math.PI / 8);
        g.drawString("西南", offsetX * 2, offsetY);
        g.rotate(2.0 * Math.PI / 8);
        g.drawString("西", offsetX, offsetY);
        g.rotate(2.0 * Math.PI / 8);
        g.drawString("西北", offsetX * 2, offsetY);
        g.rotate(2.0 * Math.PI / 8);

        //绘制8条粗线
        g.rotate(-2.0 * Math.PI / 16);
        for (int i=0; i<8; ++i){
            g.rotate(i * 2.0 * Math.PI / 8);

            g.drawLine(0, 10, 0, radius2);
        }
        g.rotate(2.0 * Math.PI / 16);

        //绘制24条细线
        g.rotate(-2.0 * Math.PI / 48);
        for (int i=0; i<24; ++i){
            g.rotate(i * 2.0 * Math.PI / 24);
            g.drawLine(0, radius0, 0, radius2);
        }
        g.rotate(2.0 * Math.PI / 48);

        //绘制角度
        g.setFont(degreeFont);
        offset = 0;
        for (int deg=0; deg<360; ++deg){
            if ( deg % 5 == 0 ){
                g.drawLine(0, longScaleR, 0, longScaleR-longScaleLength);
            }
            else{
                g.drawLine(0, shortScaleR, 0, shortScaleR-shortScaleLength);
            }
            if (deg % 10 == 0){
                String degStr = String.format("%03d", (deg+offset) % 360 );
                g.drawString(degStr, -12, -longScaleR-10);
            }
            g.rotate(2.0 * Math.PI / 360);
        }

        return image;
    }

    public static void main(String[] args) {
        CompassDrawer drawer = new CompassDrawer();
        RenderedImage image = drawer.draw();
        try{
            ImageIO.write(image, "png", new File(OUTPUT_PATH+"testR1.png"));
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
}
